// Copyright 2022 The Ip2Region Authors. All rights reserved.
// Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file.
//
// @Author Lion <chenxin619315@gmail.com>
// @Date   2022/07/12

package org.lionsoul.ip2region;

import org.lionsoul.ip2region.xdb.IndexPolicy;
import org.lionsoul.ip2region.xdb.Log;
import org.lionsoul.ip2region.xdb.Maker;

public class MakerTest {

    public final static Log log = Log.getLogger(MakerTest.class);

    public static void printHelp(String[] args) {
        System.out.println("ip2region xdb maker");
        System.out.println("java -jar ip2region-maker-{version}.jar [command options]");
        System.out.println("options:");
        System.out.println(" --src string    source ip text file path");
        System.out.println(" --dst string    destination binary xdb file path");
    }

    public static void genDb(String[] args) throws Exception {

        /**
         *数据源以及更新方法
         * wget "http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest" -O apnic-latest2.txt
         *
         * [root@cdal opt]# grep "apnic|CN|ipv4" apnic-latest2.txt > chinaIP.txt
         * [root@cdal opt]# grep "apnic|HK|ipv4" apnic-latest2.txt >> chinaIP.txt
         * [root@cdal opt]# grep "apnic|MO|ipv4" apnic-latest2.txt >> chinaIP.txt
         *
         */

        String srcFile = "", dstFile = "";
        int indexPolicy = IndexPolicy.Vector;
        srcFile = "d:/data/china.txt";
        dstFile = "d:/data/china_ip2regon.xdb";
        long tStart = System.currentTimeMillis();
        Maker maker = new Maker(indexPolicy, srcFile, dstFile);
        maker.init();
        maker.start();
        maker.end();

        log.infof("Done, elapsed: %d s", (System.currentTimeMillis() - tStart) / 1000);
    }

    public static void main(String[] args) {
        try {
            genDb(args);
        } catch (Exception e) {
            System.out.printf("failed running genDb: %s\n", e);
        }
    }

}
